﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace App
{
    public partial class Transaction : System.Web.UI.Page
    {
        AppLib.Team team;
        bool prerender = true;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Common.GetLoggedUser(this.Page) == null)
                Response.Redirect("Login.aspx");

            int ID = int.Parse(Server.HtmlEncode(Request.Cookies["Transaction"].Value));
            
            team = MidApp.ManageTeam.GetTeamByID(ID);

            
        }
        protected void Page_PreRender(object sender, EventArgs e)
        {
            if(prerender)
                SetDropDownListPlayer();

            RepeaterTeamMembers.DataSource = MidApp.ManageStock.GetPlayers(team.TeamID);
            RepeaterTeamMembers.DataBind();

            RepeaterChargeRequests.DataSource = MidApp.ManagePendingStockRequest.GetPendingStockRequestsPerTeam(team.TeamID);
            RepeaterChargeRequests.DataBind();

        }

        private void SetDropDownListPlayer()
        {
            var aux = MidApp.ManageIsMember.GetAllMembers(team.TeamID);
            foreach (var item in aux)
            {
                DropDownListPlayers.Items.Add(item.User.Nick);
            }
        }
        //obsluha tlacitka pro zobrazeni detailu hrace
        protected void LinkButtonUserInfo_Click(object sender, EventArgs e)
        {
            Response.Cookies["UserDetail"].Value = ((LinkButton)sender).CommandArgument;
            Response.Redirect("UserDetail.aspx");
        }
        //metoda vraci string hodnotu ID clena
        protected string GetMemberID(object sender)
        {
            var aux = (AppLib.Stock)sender;

            return aux.UserID.ToString();
        }
        //vraci jmeno tymu pro pozadavek k nabiti kreditu
        protected string GetUserName(object sender)
        {
            var aux = sender as AppLib.PendingStockRequest;

            return aux.User.Nick;
        }

        protected string GetUserAvatar(object sender)
        {
            var pom = (AppLib.PendingStockRequest)sender;
            return MidApp.ManageUser.GetUserImage(pom.UserID);
        }
        protected string GetUserEmail(object sender)
        {
            var pom = (AppLib.PendingStockRequest)sender;
            return (MidApp.ManageUser.GetUserByID(pom.UserID)).PrimaryEmail;
        }

        //metoda vraci nick clena tymu
        protected string GetMemberNick(object sender)
        {
            var aux = (AppLib.Stock)sender;

            return MidApp.ManageUser.GetUserByID(aux.UserID).Nick;
        }

        protected string GetMemberAvatar(object sender)
        {
                var pom = (AppLib.Stock)sender;
                return MidApp.ManageUser.GetUserImage(pom.UserID);
        }
        protected string GetMemberEmail(object sender)
        {
                var pom = (AppLib.Stock)sender;
                return (MidApp.ManageUser.GetUserByID(pom.UserID)).PrimaryEmail;
        }

        protected void ButtonCharge_Click(object sender, EventArgs e)
        {
            prerender = false;

            if (TextBoxCharge.Text == "" || TextBoxDescription.Text == "")
            {
                LabelInfo.Text = "Neuvedená částka nebo popisek.";
                LabelInfo.Visible = true;
                return;
            }

            int charge;
            var aux = DropDownListPlayers.SelectedItem.ToString();
            
            if(int.TryParse(TextBoxCharge.Text,out charge))
            {
                MidApp.ManageStock.ChangeCreditsBalance(MidApp.ManageUser.GetUserByNick(aux).UserID, team.TeamID, charge);
                AppLib.Detail newDetail = new AppLib.Detail();
                newDetail.Amount = charge.ToString();
                newDetail.Detail1 = TextBoxDescription.Text;
                newDetail.Date = DateTime.Today;
                MidApp.ManageDetail.CreateNewDetail(newDetail, team.TeamID, MidApp.ManageUser.GetUserByNick(aux).UserID);
            }

            TextBoxCharge.Text = "";
            TextBoxDescription.Text = "";
            LabelInfo.Text = "Uživateli " + DropDownListPlayers.SelectedItem.Value + " byl navýšen kredit";
            LabelInfo.Visible = true;
        }
        //zrusit zadost kreditu
        protected void LinkButtonCancel_Click(object sender, EventArgs e)
        {
            prerender = false;

            var linkbtn = sender as LinkButton;
            var aux = int.Parse(linkbtn.CommandArgument);
            MidApp.ManagePendingStockRequest.DeleteRequest(aux);
        }
        //prijme zadost o navyseni kreditu
        protected void LinkButtonConfirm_Click(object sender, EventArgs e)
        {
            prerender = false;

            var linkbtn = sender as LinkButton;
            var ChargeID = int.Parse(linkbtn.CommandArgument);
            var request = MidApp.ManagePendingStockRequest.GetRequest(ChargeID);
            var newDetail = new AppLib.Detail() {Amount=request.ChargeRequest.ToString(),Detail1="Přijatá nahlášená částka.",Date=DateTime.Now };

            MidApp.ManageStock.ChangeCreditsBalance(request.UserID, request.TeamID, request.ChargeRequest);
            MidApp.ManageDetail.CreateNewDetail(newDetail,request.TeamID, request.UserID);
            MidApp.ManagePendingStockRequest.DeleteRequest(request.ChargeID);

        }
    }
}